<?php
$order = array('2011-04-05/9_13','2011-04-05/13_17','2011-04-05/17_19'); // 7_9,9_13,13_17,17_19,19_21
$name ='11028777';
$pass = '03150';
// $cookie_jar = ('E:/testbyfnsoxt/wlyc/tmp/wlyc_cookie');
$cookie_jar = ('/tmp/wlyc_cookie');
$options = array(
    CURLOPT_COOKIEJAR => $cookie_jar,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER => false,
    CURLOPT_FAILONERROR => true,
    CURLOPT_TIMEOUT => 5,
    CURLOPT_USERAGENT=>'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.9) Gecko/20100824 Firefox/3.6.9',
);
date_default_timezone_set('Asia/Shanghai');

if(file_exists($cookie_jar)) unlink($cookie_jar);
login($name,$pass,$cookie_jar,$options);
$result = request(array(CURLOPT_URL=>'http://114.251.109.215/wlyc/aspx/car/XYYC22.aspx',CURLOPT_COOKIEFILE=>$cookie_jar)+$options);
while(true){ // echo "#33FF99:可约车"; echo "#FF0066:已约车"; echo "#00FFFF:不可约车";
if(empty($order)) break;
foreach($order as $key => $value){
    list($date,$interval) = explode('/',$value);
    $startat= explode('_',$interval);
    if((strtotime("{$date} {$startat[0]}:00") - time()-24*60*60)<0){
        echo date('Y-m-d H:i:s')."\t".$name."\t".$value."\t"."此约车时间已过\n";
        unset($order[$key]); continue;
    }
    elseif((int)((strtotime($date)-time())/3600/24)>=6){
        echo date('Y-m-d H:i:s')."\t".$name."\t".$value."\t"."此约车时间未到\n";
        continue;
    }
    $id= (int)((strtotime($date)-time())/3600/24) + 3;
    $id_str = "gvSd_ctl0{$id}_I_HOUR{$interval}"; $id_name = 'gvSd\$ctl0'.$id.'\$I_HOUR'.$interval; $id_name2 = 'gvSd$ctl0'.$id.'$I_HOUR'.$interval;
    if($result['info']['http_code'] != '200'){
        echo date('Y-m-d H:i:s')."\t".$name."\t".$value."\t".$result['info']['http_code']."\t".$result['info']['total_time']."\n";
        login($name,$pass,$cookie_jar,$options);
        $result = request(array(CURLOPT_URL=>'http://114.251.109.215/wlyc/aspx/car/XYYC22.aspx',CURLOPT_COOKIEFILE=>$cookie_jar)+$options);
        continue;
    }
    $re = '#'.$id_name.'" value="(.*?)" onclick="return checkwall\(\);" id="'.$id_str.'" style="background-color:(.*?);height:30px;width:50px;" />#iU';
    preg_match_all($re,$result['response'],$status);
    $num = $status[1][0];
    $color = $status[2][0];
    if($color == '#FF0066'){
        echo date('Y-m-d H:i:s')."\t".$name."\t".$value."\t"."已约到车\n";
        unset($order[$key]); continue;
    }
    echo date('Y-m-d H:i:s')."\t".$name."\t".$value."\t".$color."\t".$num."\t".$result['info']['http_code']."\t".$result['info']['total_time']."\n";
    preg_match('#__VIEWSTATE" value="(.*?)" />#iU',$result['response'],$vs);
    preg_match('#__EVENTVALIDATION" value="(.*?)" />#iU',$result['response'],$ev);
    $request = '__EVENTVALIDATION='.urlencode($ev[1]).'&__VIEWSTATE='.urlencode($vs[1])."&{$id_name2}={$num}&RadioButtonList1=".urlencode('散段');
    $result = request(array(CURLOPT_URL=>'http://114.251.109.215/wlyc/aspx/car/XYYC22.aspx',CURLOPT_POST=>true,CURLOPT_COOKIEFILE=>$cookie_jar,CURLOPT_POSTFIELDS=>$request)+$options);
}
if(date("H") != "07" && date("H")!="09"){
    echo "sleep 3s\n"; sleep(3);
}
}
// echo str_replace('<head>','<head><base href="http://114.251.109.215/wlyc/aspx/car/" />',$result['response']);

function request($args){
    $ch = curl_init();
    curl_setopt_array($ch, $args);
    $return = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    while($return === false){
        return request($args);
    }
    return array('response' => $return, 'info' => $info);
}

function login($name,$pass,$cookie_jar,$options){
    $return = request(array(CURLOPT_URL => 'http://114.251.109.215/wlyc/') + $options);
    preg_match('#__VIEWSTATE" value="(.*?)" />#iU',$return['response'],$vs);
    preg_match('#__EVENTVALIDATION" value="(.*?)" />#iU',$return['response'],$ev);
    request(array(CURLOPT_URL => 'http://114.251.109.215/wlyc/image.aspx')+$options);
    $captcha = (trim(array_pop(explode("\t",array_pop(file($cookie_jar))))));
    $request = '__EVENTVALIDATION='.urlencode($ev[1]).'&__VIEWSTATE='.urlencode($vs[1])."&txtname={$name}&txtpwd={$pass}&yanzheng=".trim($captcha).'&button.x=18&button.y=15';
    request(array(CURLOPT_URL=>'http://114.251.109.215/wlyc/XYYC21DR1.aspx',CURLOPT_POST=>true,CURLOPT_COOKIEFILE=>$cookie_jar,CURLOPT_POSTFIELDS=>$request)+$options);
}

function vcurl($url, $post = '', $cookie = '', $cookiejar ='', $referer =''){   
    $tmpInfo = '';   
    $cookiepath = getcwd().'./'.$cookiejar;   
    $curl = curl_init();   
    curl_setopt($curl, CURLOPT_URL, $url);   
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);   
    if($referer) {   
        curl_setopt($curl, CURLOPT_REFERER, $referer);   
    } else {   
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);    
    }   
    if($post) {   
        curl_setopt($curl, CURLOPT_POST, 1);    
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post);   
    }   
    if($cookie) {   
        curl_setopt($curl, CURLOPT_COOKIE, $cookie);   
    }   
    if($cookiejar) {   
        curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiepath);   
        curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiepath);   
    }   
    //curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);   
    curl_setopt($curl, CURLOPT_TIMEOUT, 100);   
    curl_setopt($curl, CURLOPT_HEADER, 0);   
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);   
    $tmpInfo = curl_exec($curl);   
    if (curl_errno($curl)) {   
        echo '<pre><b>错误:</b><br />'.curl_error($curl);   
    }   
    curl_close($curl);   
    return $tmpInfo;   
}   
